﻿using System.Collections.Generic;

namespace Filters.Demo.CustomFilters
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using AForge.Imaging;
    using AForge.Imaging.Filters;

    /// <summary>
    /// RotateFlipFilter class
    /// </summary>
    public class RotateFlipFilter : IFilter, IFilterInformation
    {
        private readonly RotateFlipType rotateFlipType;
        private Dictionary<PixelFormat, PixelFormat> formatTransalations = new Dictionary<PixelFormat, PixelFormat>();

        public RotateFlipFilter(RotateFlipType rotateFlipType)
        {
            this.rotateFlipType = rotateFlipType;

            // initialize format translation dictionary
            this.formatTransalations[PixelFormat.Alpha] = PixelFormat.Alpha;
            this.formatTransalations[PixelFormat.Canonical] = PixelFormat.Canonical;
            this.formatTransalations[PixelFormat.DontCare] = PixelFormat.DontCare;
            this.formatTransalations[PixelFormat.Extended] = PixelFormat.Extended;
            this.formatTransalations[PixelFormat.Gdi] = PixelFormat.Gdi;
            this.formatTransalations[PixelFormat.Indexed] = PixelFormat.Indexed;
            this.formatTransalations[PixelFormat.Max] = PixelFormat.Max;
            this.formatTransalations[PixelFormat.PAlpha] = PixelFormat.PAlpha;
            this.formatTransalations[PixelFormat.Undefined] = PixelFormat.Undefined;

            this.formatTransalations[PixelFormat.Format16bppArgb1555] = PixelFormat.Format16bppArgb1555;
            this.formatTransalations[PixelFormat.Format16bppGrayScale] = PixelFormat.Format16bppGrayScale;
            this.formatTransalations[PixelFormat.Format16bppRgb555] = PixelFormat.Format16bppRgb555;
            this.formatTransalations[PixelFormat.Format16bppRgb565] = PixelFormat.Format16bppRgb565;

            this.formatTransalations[PixelFormat.Format1bppIndexed] = PixelFormat.Format1bppIndexed;

            this.formatTransalations[PixelFormat.Format24bppRgb] = PixelFormat.Format24bppRgb;

            this.formatTransalations[PixelFormat.Format32bppArgb] = PixelFormat.Format32bppArgb;
            this.formatTransalations[PixelFormat.Format32bppPArgb] = PixelFormat.Format32bppPArgb;
            this.formatTransalations[PixelFormat.Format32bppRgb] = PixelFormat.Format32bppRgb;

            this.formatTransalations[PixelFormat.Format48bppRgb] = PixelFormat.Format48bppRgb;

            this.formatTransalations[PixelFormat.Format4bppIndexed] = PixelFormat.Format4bppIndexed;

            this.formatTransalations[PixelFormat.Format64bppArgb] = PixelFormat.Format64bppArgb;
            this.formatTransalations[PixelFormat.Format64bppPArgb] = PixelFormat.Format64bppPArgb;

            this.formatTransalations[PixelFormat.Format8bppIndexed] = PixelFormat.Format8bppIndexed;
        }

        public Bitmap Apply(Bitmap image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image", "The image parameter cannot be null.");
            }

            var newImage = image.Clone() as Bitmap;
            if (newImage != null)
            {
                newImage.RotateFlip(this.rotateFlipType);
            }

            return newImage;
        }

        public Bitmap Apply(BitmapData imageData)
        {
            throw new NotImplementedException();
        }

        public UnmanagedImage Apply(UnmanagedImage image)
        {
            throw new NotImplementedException();
        }

        public void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            throw new NotImplementedException();
        }

        public Dictionary<PixelFormat, PixelFormat> FormatTransalations
        {
            get { return this.formatTransalations; }
        }
    }
}
